package com.spzx.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.domain.Product;
import com.spzx.product.api.domain.ProductDetails;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.SkuStock;
import com.spzx.product.api.domain.query.ProductSkuQuery;
import com.spzx.product.api.domain.vo.ProductSkuVO;
import com.spzx.product.api.domain.vo.SkuPriceVO;
import com.spzx.product.api.domain.vo.SkuStockVO;
import com.spzx.product.api.domain.vo.SpecValueVO;
import com.spzx.product.mapper.ProductDetailsMapper;
import com.spzx.product.mapper.ProductSkuMapper;
import com.spzx.product.service.ProductDetailsService;
import com.spzx.product.service.ProductService;
import com.spzx.product.service.ProductSkuService;
import com.spzx.product.service.SkuStockService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, ProductSku> implements ProductSkuService {

    @Lazy
    @Resource
    ProductService productService;
    @Resource
    SkuStockService skuStockService;
    @Resource
    ProductDetailsService productDetailsService;

    @Override
    public List<ProductSku> selectIndexSkus() {
        return baseMapper.selectIndexSkus();
    }

    @Override
    public Page<ProductSku> selectListSkus(ProductSkuQuery query) {
        //1、封装分页条件对象
        Page<ProductSku> page = new Page<>(query.getPageNo(), query.getPageSize());
        //=== 2、通过QueryWrapper组装条件: 多表联查时，queryWrapper不是特别方便使用

        //3、传入query条件对象
        List<ProductSku> list = baseMapper.selectListSkus(page, query);
        //4、将查询的数据集合设置给page对象
        page.setRecords(list);
        return page;
    }

    @Override
    public ProductSkuVO selectSkuDetails(Long skuId) {
        //  1 根据id查询
        ProductSku productSku = this.getById(skuId);
        if (productSku == null) {
            throw new ServiceException("商品数据不存在");

        }

        //2封装skuPrice
        SkuPriceVO skuPriceVO = new SkuPriceVO();
        skuPriceVO.setSkuId(skuId);
        skuPriceVO.setMarketPrice(productSku.getMarketPrice());
        skuPriceVO.setSalePrice(productSku.getSalePrice());
        //3、根据sku的productId查询spu
        Long productId = productSku.getProductId();
        Product product = productService.getById(productId);
        //4、根据spu的规格属性值解析得到 SpecValueList  & 以及轮播图 sliderUrlList
        //轮播图
        List<String> sliderUrlList = StringUtils.isEmpty(product.getSliderUrls())
                ? null : Arrays.asList(product.getSliderUrls().split(","));
        //spu的规格参数值
        List<SpecValueVO> specValueVOS = JSON.parseArray(product.getSpecValue(), SpecValueVO.class);
        //5、根据sku的id查询自己的库存  SkuStockVo
        SkuStock skuStock = skuStockService.getOne(Wrappers.lambdaQuery(SkuStock.class)
                .eq(SkuStock::getSkuId, skuId).last("limit 1 "));
        SkuStockVO skuStockVO = new SkuStockVO();
        skuStockVO.setSkuId(skuId);
        skuStockVO.setSaleNum(skuStockVO.getSaleNum());////???
        skuStockVO.setAvailableNum(skuStock.getAvailableNum());///??????
        //6、根据productId查询 detailsImageUrlList 详情图列表
        ProductDetails productDetails = productDetailsService.getById(productId);
        List<String> detailsImageUrlList = productDetails != null ? Arrays.asList(productDetails.getImageUrls().split(",")) : null;
        //7、根据productId查询它所有的sku集合
        List<ProductSku> skus = this.list(Wrappers.lambdaQuery(ProductSku.class).eq(ProductSku::getProductId, productId)
                .eq(ProductSku::getStatus, 1));
        //8、将sku集合转为 specValue映射skuId的map集合
        Map<String, Long> map = skus.stream().collect(Collectors.toMap(ProductSku::getSkuSpec, ProductSku::getId));
        //9、将上面的数据组装为 ProductSkuVO对象
        ProductSkuVO skuVO = new ProductSkuVO();
        skuVO.setProductSku(productSku);
        skuVO.setSkuPrice(skuPriceVO);
        skuVO.setProduct(product);
        skuVO.setSliderUrlList(sliderUrlList);
        skuVO.setDetailsImageUrlList(detailsImageUrlList);
        skuVO.setSpecValueList(specValueVOS);
        skuVO.setSkuStockVo(skuStockVO);
        skuVO.setSkuSpecValueMap(map);
        return skuVO;


    }

    @Override
    public Map<Long, BigDecimal> getSkusPrice( List<Long> skuIds){
        List<ProductSku> productSkus = this.list(Wrappers.lambdaQuery(ProductSku.class)
                .select(ProductSku::getId, ProductSku::getSalePrice)
                .in(ProductSku::getId, skuIds));
        return productSkus.stream()
                .collect(Collectors.toMap(ProductSku::getId,ProductSku::getSalePrice));
    }
}
